//
//  SLTabBar.swift
//  SLBlog
//
//  Created by  on 2017/11/23.
//  Copyright © 2017 pan. All rights reserved.
//

import UIKit

class SLTabBar: UITabBar {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //add subView
        addSubview(plusBtn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let itemW = self.frame.width / 5
        let itemH = self.bounds.height
        
        let rect = CGRect(x: 0, y: 0, width: itemW, height: itemH)
        
        var index: CGFloat = 0
        
        for subview in subviews {
            
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                subview.frame = rect.offsetBy(dx: index * itemW, dy: 0)
                index += (index == 1) ? 2 : 1
            }
        }
        
        plusBtn.frame = rect.offsetBy(dx: 2 * itemW, dy: 0)
        
    }

    //lazy load plusBtn
    lazy var plusBtn: UIButton = {
        
        let btn = UIButton()
        
        //set background image
        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        
        //set image
        btn.setImage(UIImage.init(named: "tabbar_compose_icon_add"), for: UIControlState.normal)
        btn.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        
        return btn
    }()
}
